#include "../include/lib_headers.h"
void traversal(node *n) {

	if (n == NULL)
		return;
	else {
		printf(
			"n->fullPath= %s n->fileName= %s Address=%p Sibling=%p Child=%p fileType=%s Block_No=%d\n",
			n->fd_pointer->fullPath, n->fd_pointer->fileName, n, n->sibling,
			n->child, n->fd_pointer->fileType, n->fd_pointer->blockno);
		traversal(n->sibling);
		traversal(n->child);
	}
}
void unmount_nAry(node *n) {
	if (n == NULL)
		return;
	else {
		unmount_nAry(n->child);
		unmount_nAry(n->sibling);
		/* printf("node name is : %s\n",n->fd_pointer->fileName); */
		free(n->fd_pointer);
		free(n);
	}
}

void list_traversal(node *child, char *to_write) {
	char str[LARGESIZE] = "";
	if (child == NULL)
		return;
	else {
		sprintf(str, "FileName = %s FullPath =  %s FileType =  %s Block No = %d\n", child->fd_pointer->fileName,
			child->fd_pointer->fullPath, child->fd_pointer->fileType,
			child->fd_pointer->blockno);
		strcat(to_write, str);
		/* printf("%s %s %s %d %p\n", child->fd_pointer->fileName,
			child->fd_pointer->fullPath, child->fd_pointer->fileType,
			child->fd_pointer->blockno, child); */
		list_traversal(child->sibling, to_write);	
		list_traversal(child->child, to_write);				
	}
}

void traversal1(node *n, char *deleted_text, char *dest) {
	int count = 0;
	char str[SIZE] = "";
	strcpy(str, dest);
	if (n == NULL)
		return;
	else {
		/* printf("strip_name(n->fd_pointer->fullPath,deleted_text) = %s\n", 
			strip_name(n->fd_pointer->fullPath, deleted_text));
		printf("n->fd_pointer->fullPath = %s deleted_text = %s \n", 
			n->fd_pointer->fullPath, deleted_text); */
		strcpy(n->fd_pointer->fullPath, strcat(str, strip_name(n->fd_pointer->fullPath, deleted_text)));
		traversal1(n->sibling, deleted_text, dest);
		traversal1(n->child, deleted_text, dest);

	}
}

void append_traversal(node *parent, node *n, char *deleted_text, char *dest) {
	int count = 0;
	//char str[SIZE] = "";
	char var[SIZE]="";
	//strcpy(str, dest);
	if (n == NULL || parent ==NULL || parent->child == NULL )
		return;
	else {
		/*printf("strip_name(n->fd_pointer->fullPath,deleted_text) = %s\n", 
			strip_name(n->fd_pointer->fullPath, deleted_text));
		printf("n->fd_pointer->fullPath = %s deleted_text = %s \n", 
			n->fd_pointer->fullPath, deleted_text); */
			
		strcpy(var, deleted_text);
		strcat(var,"/");
		strcat(var,n->fd_pointer->fileName);
		/*printf("parent->child->fd_pointer->fullPath = %s\n", parent->child->fd_pointer->fullPath );	
		printf("n->fd_pointer->fullPath = %s\n", n->fd_pointer->fullPath );	
		printf("var = %s\n", var);
		printf("2------------------\n");*/
		del(n->fd_pointer->fullPath);
		htable_delete(hashtable, *(n->fd_pointer));		
		strcpy(n->fd_pointer->fullPath, var);
		insert(n->fd_pointer);
		htable_insert(hashtable, *(n->fd_pointer));
		if ( n != NULL || n->sibling != NULL)
		append_traversal(parent,n->sibling, deleted_text, dest);
		if( parent !=NULL || parent->child != NULL || n == NULL)
		append_traversal(parent->child,n->child, parent->child->fd_pointer->fullPath, dest);
		return;
	}
}

void traverse_file(node *n) {

	if (n == NULL)
		return;
	else {
		if (strcmp(n->fd_pointer->fileType, "file") == 0)
			printf(
					"n->Absolute= %s n->Relative= %s Address=%p Sibling=%p Child=%p Type=%s\n",
					n->fd_pointer->fullPath, n->fd_pointer->fileName, n, n->sibling, n->child, n->fd_pointer->fileType);
		traversal(n->sibling);
		traversal(n->child);

	}
}

void traverse_write(node *n) {
	if (n == NULL){	
		return;
	}	
		array[cnt++]=*(n->fd_pointer);
		/* printf(" cnt = %d n = %p\n", cnt,n); */
		traverse_write(n->sibling);
		traverse_write(n->child);
			
}

